package com.gxa.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.admin.mapper.UserMapper;
import com.gxa.admin.pojo.dto.UserDto;
import com.gxa.admin.pojo.vo.UserVo;
import com.gxa.admin.service.UserService;
import com.gxa.admin.utils.QueryResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lch
 * @date 2024/3/21 10:17
 * @description 会员信息实现类
 **/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    /**
     * 查询所有会员信息
     * @param userDto
     * @return
     */
    @Override
    public QueryResult getAllUser(UserDto userDto) {

        //分页查询
        //import com.github.pagehelper.Page;
        //import com.github.pagehelper.PageHelper;
        Page<UserVo> page=PageHelper.startPage(userDto.getPage(), userDto.getLimit());

        //查询会员等级
        List<UserVo> userVoList = this.userMapper.queryAllUser(userDto);

        //封装返回数据
        QueryResult queryResult = new QueryResult();
        queryResult.setData(userVoList);
        queryResult.setCount(page.getTotal());

        return queryResult;
    }


    /**
     * 添加会员信息
     * @param userDto
     * @return
     */
    @Override
    public int addUser(UserDto userDto) {

        //根据积分设置会员等级
        setVipId(userDto);

        //获取当前时间
        Date date=new Date(System.currentTimeMillis());
        userDto.setUserBirth(date);

        return this.userMapper.insertUser(userDto);
    }


    /**
     * 更新会员信息
     * @param userDto
     * @return
     */
    @Override
    public int updateUser(UserDto userDto) {

        //根据积分设置会员等级
        setVipId(userDto);

        return this.userMapper.updateUser(userDto);
    }


    /**
     * 根据id删除会员信息
     * @param userId
     * @return
     */
    @Override
    public int deleteUser(Integer userId) {

        //判断id是否为空
        if(userId==null){
            return -1;
        }
        return this.userMapper.deleteUser(userId);
    }


    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public UserVo getUserById(Integer userId) {

        //判断id是否为空
        if(userId==null){
            return null;
        }
        return this.userMapper.queryUserById(userId);
    }


    /**
     * 根据积分设置会员等级
     * @param userDto
     * @return
     */
    public void setVipId(UserDto userDto){

        if (userDto.getUserScore()==null){
            //积分为空，新用户
            userDto.setUserScore(0);
            userDto.setUserVipId(5);
        }
        else if (userDto.getUserScore() >= 10000) {
            //至尊会员
            userDto.setUserVipId(4);
        } else if (userDto.getUserScore() >= 5000) {
            //钻石会员
            userDto.setUserVipId(3);
        } else if (userDto.getUserScore() >= 1000) {
            //黄金会员
            userDto.setUserVipId(2);
        } else if (userDto.getUserScore() >= 100) {
            //普通会员
            userDto.setUserVipId(1);
        }
    }

}
